﻿using System;
using System.Collections.Generic;
using NHibernate.Criterion;
using EntitiesAndMappings.Entities;
using NHibernate.SqlCommand;

namespace ProyectoActivos.Daos
{
    public class SubFamiliaDAO : BaseDAO<SubFamilia>
    {
        public IList<SubFamilia> ListarActivos()
        {
            IList<SubFamilia> lista;
            using (var session = CreateSessionFactory().OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    var criteria = session.CreateCriteria<SubFamilia>();
                    criteria.Add(Restrictions.Eq("Estado", "A"));
                    lista = criteria.List<SubFamilia>();
                    
                    transaction.Commit();
                }
            }
            return lista;
        }

        public IList<SubFamilia> ListarActivosPorFamilia(int idFamilia)
        {
            IList<SubFamilia> lista;
            using (var session = CreateSessionFactory().OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    var criteria = session.CreateCriteria<SubFamilia>();
                    criteria.Add(Restrictions.Eq("Estado", "A"));
                    criteria.Add(Restrictions.Eq("Familia.Id", idFamilia));
                    lista = criteria.List<SubFamilia>();

                    transaction.Commit();
                }
            }
            return lista;
        }
             


        public IList<SubFamilia> Buscar(SubFamilia subFamilia)
        {
            IList<SubFamilia> lista;
            using (var session = CreateSessionFactory().OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    var criteria = session.CreateCriteria(typeof(SubFamilia), "SubFamilia")
                            .CreateAlias("Familia", "Familia", JoinType.InnerJoin)
                            .Add(Expression.InsensitiveLike("SubFamilia.Codigo", subFamilia.Codigo, MatchMode.Start));
                            
                    lista = criteria.List<SubFamilia>();
                    transaction.Commit();
                }

            }
            return lista;
        }


        public Familia ObtenerFamilia(SubFamilia subFamilia)
        {
            Familia familia;
            using (var session = CreateSessionFactory().OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    var criteria = session.CreateCriteria(typeof(SubFamilia), "SubFamilia")
                            .CreateAlias("Familia", "Familia", JoinType.InnerJoin)
                            .Add(Restrictions.Eq("SubFamilia.Id", subFamilia.Id));
                    familia = criteria.UniqueResult<SubFamilia>().Familia;
                    transaction.Commit();
                }
            }
            return familia;
        }



    }
}
